package com.boyu.front;

import java.util.UUID;

import com.boyu.common.BaseController;
import com.boyu.common.SystemConst;
import com.boyu.common.model.Fuser;
import com.boyu.common.utils.MailUtils;
import com.boyu.common.utils.StringUtils;
import com.boyu.service.FuserService;
import com.jfinal.aop.Duang;

/**
 * @author changan.hca
 * @date 2017/7/2
 */
public class FrontUserController extends BaseController {
    protected FuserService fuserService = Duang.duang(FuserService.class);

    /**
     * 跳转到注册页面
     */
    public void reg() {
        render("reg.html");
    }

    /**
     * 跳转到登录页面
     */
    public void login() {
        render("login.html");
    }

    /**
     * 跳转到找回密码页面
     */
    public void findpw() {
        this.setAttr("type", this.getPara("t"));
        render("findpw.html");
    }

    /**
     * 提交注册信息
     */
    public void ureg() {
        Fuser fuser = this.getModel(Fuser.class, "");
        String pwdMd5 = StringUtils.encoderByMd5(fuser.getPassword());
        fuser.setPassword(pwdMd5);
        fuser.save();
        setSessionAttr(SystemConst.FRONT_USER_SESSION, fuser);
        redirect("/");
    }

    /**
     * 登录
     */
    public void ulogin() {
        String username = this.getPara("username");
        String password = this.getPara("password");
        Fuser fuser = fuserService.get(username);
        if (fuser == null) {
            this.setAttr("msg", "用户不存在");
            render("tip.html");
        } else {
            String pwdMd5 = StringUtils.encoderByMd5(password);
            if (!pwdMd5.equals(fuser.getPassword())) {
                this.setAttr("msg", "密码错误");
                render("tip.html");
            } else {
                setSessionAttr(SystemConst.FRONT_USER_SESSION, fuser);
                redirect("/");
            }
        }
    }

    /**
     * 找回密码
     */
    public void ufindpw() {
        //type 1使用用户名和邮箱找回   2使用密保找回
        String type = this.getPara("type");

        String tip = null;
        String username = this.getPara("username");
        if (StringUtils.isBlank(username)) {
            this.setAttr("msg", "用户名不能为空");
            render("tip.html");
            return;
        }
        Fuser fuser = fuserService.get(username);
        if (fuser == null) {
            this.setAttr("msg", "用户名不存在");
            render("tip.html");
            return;
        }

        if ("1".equals(type)) {
            String email = this.getPara("email");
            if (StringUtils.isNotBlank(email) && fuser.getEmail().equals(email)) {
                tip = "邮件已经发送，请注意查收";
                String findpwKey = UUID.randomUUID().toString();
                fuser.setFindpwKey(findpwKey);
                fuser.save();
                MailUtils.send(email, "找回密码", "请点击下面的链接，进行重置密码</br> username=" + username + "&findpwKey=" + findpwKey);
            } else {
                tip = "用户跟邮箱不匹配";
            }
        } else if ("2".equals(type)) {
            String pw_question = this.getPara("pw_question");
            String pw_answer = this.getPara("pw_answer");
            if (StringUtils.isBlank(pw_question) || StringUtils.isBlank(pw_answer)) {
                tip = "密保不对，无法找回密码";
            } else {
                if (pw_answer.equals(fuser.getPwAnswer()) && pw_question.equals(fuser.getPwQuestion())) {
                    String findpwKey = UUID.randomUUID().toString();
                    fuser.setFindpwKey(findpwKey);
                    fuser.save();
                    this.setAttr("username", username);
                    this.setAttr("findpwKey", findpwKey);
                } else {
                    tip = "密保不对，无法找回密码";
                }
            }
        } else {
            tip = "无法找回密码";
        }
        if (tip == null) {
            render("resetpw.html");
        } else {
            this.setAttr("msg", tip);
            render("tip.html");
        }
    }

    /**
     * 重置密码
     */
    public void uresetpw() {
        String username = this.getPara("username");
        String findpwKey = this.getPara("findpwKey");
        String password = this.getPara("password");
        if (StringUtils.isBlank(username) || StringUtils.isBlank(findpwKey)) {
            this.setAttr("msg", "用户名不能为空");
            render("tip.html");
            return;
        }
        if (StringUtils.isBlank(password)) {
            this.setAttr("msg", "新密码不能为空");
            render("tip.html");
            return;
        }
        Fuser fuser = fuserService.get(username);
        if (fuser == null) {
            this.setAttr("msg", "用户名不存在");
            render("tip.html");
            return;
        }

        if (!findpwKey.equals(fuser.getFindpwKey())) {
            this.setAttr("msg", "密码重置失败，秘钥已经失效");
            render("tip.html");
            return;
        }
        String pwdMd5 = StringUtils.encoderByMd5(fuser.getPassword());
        fuser.setPassword(pwdMd5);
        fuser.save();
        this.setAttr("msg", "密码重置成功");
        render("tip.html");
    }

    /**
     * 用户名是否注册过
     */
    public void isReg() {
        String username = this.getPara("username");
        Fuser fuser = fuserService.get(username);
        if (fuser == null) {
            this.renderText("true");
        } else {
            this.renderText("false");
        }

    }

    /**
     * 邮箱是否注册过
     */
    public void isEmail() {
        String email = this.getPara("email");
        Fuser fuser = fuserService.getByEmail(email);
        if (fuser == null) {
            this.renderText("true");
        } else {
            this.renderText("false");
        }

    }
}
